import 'package:demo_app_video/commons/router/routes.dart';
import 'package:demo_app_video/page/drama_details/drama_details.dart';
import 'package:flutter/material.dart';
import 'package:flutter_adcontent/flutter_adcontent.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class VideoCardPage extends StatefulWidget {
  VideoCardPage({Key? key,this.controller}) : super(key: key);

  final ProVideoController? controller;


  @override
  State<VideoCardPage> createState() => _VideoCardPageState();
}

class _VideoCardPageState extends State<VideoCardPage> with AutomaticKeepAliveClientMixin{

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;


  @override
  void initState() {

    // TODO: implement initState
    super.initState();

  }

  
 

  @override
  Widget build(BuildContext context) {
    super.build(context);
    // 获取屏幕宽高
    Size size = MediaQuery.of(context).size;
    // 获取屏幕安全距离
    EdgeInsets padding = MediaQuery.of(context).padding;

    return Scaffold(
        body: LayoutBuilder(
        builder: (context, constraints) {
          print('build constraints: $constraints');
          // 获取 body 的大小
          var bodyWidth = constraints.maxWidth;
          var bodyHeight = constraints.maxHeight;

          return TheaterWidget(
            controller: widget.controller,
            width: bodyWidth,
            height: bodyHeight,
            channelType: VideoDrawParams.drawChannelTypeRecommend,
            hideTopInfo: true,
            setTopOffset: 54,
            hideBottomInfo: true,
            hideFavorButton: true,
            hideLikeButton: true,
            theaterListener: TheaterListener(
              onOpenDetail: (drama) async{
                // 打开短剧详情
                await toDetail(context, drama);

                print('====>回调');
                print('====>回调');
                print('====>回调');
                
              },
            ),
          );
        },
      ));
  }

  Future<T?> toDetail<T extends Object?>(
      BuildContext context, Drama drama) async {


    var resultData = await FlutterAdcontent.requestDrama([drama.id]);
    await widget.controller!.pause();
    Routes().push(context, Routes.dramaDetails, {
      'drama':resultData.first
    },callback: (value) async{
      await widget.controller!.resume();
    });
  }
}
